package com.taptech.doufu.ui.viewholder.personalcenter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.NewNovelCreateListTosActivity;
import com.taptech.doufu.manager.NovelEditManager;
import com.taptech.doufu.bean.MineAbstractBean;
import com.taptech.doufu.bean.NovelSectionBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.ui.view.drawcircle.BaseRecyclerViewViewHolder;
import com.taptech.doufu.bean.TipInfo;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.listener.ResultTipListener;
import com.taptech.doufu.services.NovelCreateServices;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.ui.view.TipDialog;
import com.taptech.doufu.ui.view.WaitDialog;

/**
 * Created by longsiyang on 15/10/27.
 */
public class NovelSectionRecyclerViewHolder extends BaseRecyclerViewViewHolder implements View.OnClickListener , HttpResponseListener{


    Context mContext;
    RelativeLayout novelSectionLayout;
    TextView novelSectionTitle;
    TextView novelSectionUpdateTime;
    TextView novelSectionWordCount;
    TextView novelSectionDeleteBtn;

    TextView topTitleText;

    NovelSectionBean bean;
    WaitDialog dialog;


    public NovelSectionRecyclerViewHolder(Context context, View itemView) {
        super(context, itemView);
        mContext = context;
        novelSectionLayout = (RelativeLayout) itemView.findViewById(R.id.section_back_layout);
        novelSectionLayout.setOnClickListener(this);
        novelSectionTitle = (TextView) itemView.findViewById(R.id.novel_create_list_item_title);
        novelSectionUpdateTime = (TextView) itemView.findViewById(R.id.novel_create_list_item_time);
        novelSectionWordCount = (TextView) itemView.findViewById(R.id.section_word_count);
        novelSectionDeleteBtn = (TextView) itemView.findViewById(R.id.section_delete_btn);
        novelSectionDeleteBtn.setOnClickListener(this);
        topTitleText = (TextView) itemView.findViewById(R.id.novel_create_list_item_sent_tag);
        dialog = new WaitDialog(mContext, R.style.loadNovelDialog, "");
        dialog.setCanceledOnTouchOutside(false);
    }



    @Override
    public void setViewHolderChildViewContent(MineAbstractBean tempbean, int position) {
        bean = (NovelSectionBean) tempbean;
        if (bean == null){
            return;
        }
        if (bean.getTitle() != null){
            novelSectionTitle.setText(bean.getTitle());
        }
        if (bean.getUpdate_time() != null){
            novelSectionUpdateTime.setText("发布：" + DiaobaoUtil.seconds2TimeString(Long.parseLong(bean.getUpdate_time())));
        }
        if (bean.getLength() != null){
            novelSectionWordCount.setText(bean.getLength() + " 字");
        }
//        if (bean.isSpecialFlag()){
//            topTitleText.setVisibility(View.VISIBLE);
//        }else {
//            topTitleText.setVisibility(View.GONE);
//        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.section_back_layout:
                if (bean == null){
                    return;
                }
                NovelCreateServices.enterNovelCreateContentTos(mContext, NovelEditManager.NOVEL_SECTION,bean.getTitle(), bean.getId(), bean.getTopicId(), bean.getId(),null, bean.getUser().getIcon());

                break;
            case R.id.section_delete_btn:
                TipInfo info = new TipInfo();
                info.setTipContent("是否删除章节？");
                info.setTipListener(tipListener);
                TipDialog dialog = new TipDialog(mContext, R.style.updateDialog, info);
                dialog.show();
                break;
        }
    }

    private ResultTipListener tipListener = new ResultTipListener() {

        @Override
        public void resultBack(int flag, int type) {
            if(1 == flag){
                if(dialog != null && !dialog.isShowing()){
                    dialog.show();
                }
                NovelCreateServices.getInstance().deleteNovelSectionRequest(NovelSectionRecyclerViewHolder.this, bean.getId());
            }
        }
    };

    @Override
    public void handleResponse(int handleType, HttpResponseObject response) {
        if(dialog != null && dialog.isShowing()){
            dialog.dismiss();
        }
        switch (handleType){
            case NovelCreateServices.HANDLE_TYPE_DEL_NOVEL_SECTION:
                if (response != null){
                    if (response.getStatus() == Constant.STATUS_SUCCESS && mHandler != null){
//                        mHandler.sendEmptyMessage(NewNovelCreateListActivity.REFRESH_REQUEST_CODE);
                        mHandler.sendEmptyMessage(NewNovelCreateListTosActivity.REFRESH_REQUEST_CODE);
                    }else if (!TextUtils.isEmpty(response.getUser_msg())){
                        Toast.makeText(mContext,response.getUser_msg(),Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(mContext,"删除失败，请检查您的网络再重试",Toast.LENGTH_LONG).show();

                    }
                }
                break;
        }
    }
}
